package com.yiji.openapi;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Maps;
import com.suixun.service.Constants;

public class YijiService {
    protected static Logger logger = LoggerFactory.getLogger(YijiService.class);

    /*
     * 代扣签约
     */
    public static String sign(String orderno, String telephone, String username, String idcard,
            String bankidcard, String bankname, String logid) {
        Map<String, String> params = Maps.newHashMap();
        // 基本参数
        // 这部分参数对于一个商户而言是固定的
        params.put(ApiConstants.ORDER_NO, orderno);
        params.put(ApiConstants.PARTNER_ID, Constants.get_partner_id());
        params.put(ApiConstants.PROTOCOL, Constants.HTTP_POST_PROTOCOL);
        params.put(ApiConstants.SIGN_TYPE, Constants.SIGN_TYPE);
        params.put(ApiConstants.NOTIFY_URL, Constants.get_notify_url());
        params.put(ApiConstants.RETURN_URL, Constants.RETURN_URL);

        // 业务参数
        // 每个服务的业务参数是不同的
        // 具体请参考文档的请求参数
        params.put(ApiConstants.SERVICE, "yxtDeductSign");
        params.put("serviceCode", Constants.get_service_code());
        params.put("realName", username);
        params.put("certNo", idcard);
        params.put("certValidTime", "0");
        params.put("mobileNo", telephone);
        params.put("profession", "");
        params.put("address", "");
        params.put("bankCardType", "DEBIT_CARD"); // DEBIT_CARD:借记卡 CREDIT_CARD:信用卡
        params.put("bankCode", Constants.getBankcode(bankname));
        params.put("bankCardNo", bankidcard);

        params.put("certBackImageUrl", "1");
        params.put("memo", "qianyuedaikou");
        params.put("withholdContractNo", logid);
        params.put("address", "hangzhou");
        params.put("bankCardImageUrl", "1");
        params.put("profession", "1");
        params.put("withholdContractImageUrl", "daikouhetong");
        params.put("certFrontImageUrl", "1");
        // 发送请求
        String responseMessage = HttpRequest.exchange(params);

        // 反序列化数据
        @SuppressWarnings("unchecked")
        HashMap<String, String> resultMap =
                JsonMapper.nonDefaultMapper().fromJson(responseMessage, HashMap.class);

        // {"sign":"abff85cc07c609ff106868ed68fa13b8","protocol":"httpPost","orderNo":"99162183fb564286f74360f704b57d82","signType":"MD5","resultCode":"EXECUTE_FAIL","service":"yxtDeductSign","partnerId":"20150818020000465881","resultMessage":"通用代扣代扣合同号重复","success":false,"version":"1.0"}
        if (resultMap.get("resultMessage") != null)
            return resultMap.get("resultMessage");
        else {
            return "";
        }
    }

    public static String deduct(String orderno, String contractNo, String externalOrderNo,
            BigDecimal totalAmount) {
        Map<String, String> params = Maps.newHashMap();
        // 基本参数
        // 这部分参数对于一个商户而言是固定的
        params.put(ApiConstants.ORDER_NO, orderno);
        params.put(ApiConstants.PARTNER_ID, Constants.get_partner_id());
        params.put(ApiConstants.PROTOCOL, Constants.HTTP_POST_PROTOCOL);
        params.put(ApiConstants.SIGN_TYPE, Constants.SIGN_TYPE);
        params.put(ApiConstants.NOTIFY_URL, Constants.get_notify_url());
        params.put(ApiConstants.RETURN_URL, Constants.RETURN_URL);

        // 业务参数
        // 每个服务的业务参数是不同的
        // 具体请参考文档的请求参数
        params.put(ApiConstants.SERVICE, "yxtDeduct");
        params.put("contractNo", contractNo);
        params.put("externalOrderNo", externalOrderNo);
        params.put("totalAmount", totalAmount.toString());
        params.put("costComposition", "{\"手表激活费用\":" + totalAmount.toString() + "}");
        params.put("memo", "随寻代扣");

        // 发送请求
        String responseMessage = HttpRequest.exchange(params);
        // 反序列化数据
        @SuppressWarnings("unchecked")
        HashMap<String, String> resultMap =
                JsonMapper.nonDefaultMapper().fromJson(responseMessage, HashMap.class);

        if (resultMap.get("resultMessage") != null)
            return resultMap.get("resultMessage");
        else {
            return "";
        }
    }

    public static String renewal(String orderno, String tradename, String payeeUserId,
            BigDecimal totalAmount, int type, int length) {
        Map<String, String> params = Maps.newHashMap();
        // 基本参数
        // 这部分参数对于一个商户而言是固定的
        params.put(ApiConstants.ORDER_NO, orderno);
        params.put(ApiConstants.PARTNER_ID, Constants.get_partner_id_web());
        params.put(ApiConstants.PROTOCOL, Constants.HTTP_POST_PROTOCOL);
        params.put(ApiConstants.SIGN_TYPE, Constants.SIGN_TYPE);
        params.put(ApiConstants.NOTIFY_URL, Constants.get_notify_url());
        params.put(ApiConstants.RETURN_URL, Constants.get_renewalfees_okurl());

        // 业务参数
        // 每个服务的业务参数是不同的
        // 具体请参考文档的请求参数
        params.put(ApiConstants.SERVICE, "fastPayCreateTrade");
        params.put("requestType", "1");
        params.put("currency", "CNY");
        params.put("goodsName", tradename);
        params.put("tradeAmount", totalAmount.toString());
        params.put("partnerUserId", payeeUserId.split("a")[0]);
        params.put("sellerUserId", Constants.get_partner_id_web());
        params.put("merchOrderNo", orderno);
        // 发送请求
        String responseMessage = HttpRequest.exchange_web(params);
        logger.error(responseMessage);
        // 反序列化数据
        @SuppressWarnings("unchecked")
        HashMap<String, String> resultMap =
                JsonMapper.nonDefaultMapper().fromJson(responseMessage, HashMap.class);
        if (resultMap.get("resultCode") != null
                && resultMap.get("resultCode").equals("EXECUTE_SUCCESS"))
            return resultMap.get("tradeNo");
        else {
            return "";
        }
    }

    public static String purchase(String orderno, String tradename, String payeeUserId,
            BigDecimal totalAmount) {
        Map<String, String> params = Maps.newHashMap();
        // 基本参数
        // 这部分参数对于一个商户而言是固定的
        params.put(ApiConstants.ORDER_NO, orderno);
        params.put(ApiConstants.PARTNER_ID, Constants.get_partner_id_web());
        params.put(ApiConstants.PROTOCOL, Constants.HTTP_POST_PROTOCOL);
        params.put(ApiConstants.SIGN_TYPE, Constants.SIGN_TYPE);
        params.put(ApiConstants.NOTIFY_URL, Constants.get_notify_url());
        params.put(ApiConstants.RETURN_URL, Constants.get_renewalfees_okurl());

        // 业务参数
        // 每个服务的业务参数是不同的
        // 具体请参考文档的请求参数
        params.put(ApiConstants.SERVICE, "fastPayCreateTrade");
        params.put("requestType", "1");
        params.put("currency", "CNY");
        params.put("goodsName", tradename);
        params.put("tradeAmount", totalAmount.toString());
        params.put("partnerUserId", payeeUserId);
        params.put("sellerUserId", Constants.get_partner_id_web());
        params.put("merchOrderNo", orderno);
        // 发送请求
        String responseMessage = HttpRequest.exchange_web(params);
        logger.info(responseMessage);
        // 反序列化数据
        @SuppressWarnings("unchecked")
        HashMap<String, String> resultMap =
                JsonMapper.nonDefaultMapper().fromJson(responseMessage, HashMap.class);
        if (resultMap.get("resultCode") != null
                && resultMap.get("resultCode").equals("EXECUTE_SUCCESS"))
            return resultMap.get("tradeNo");
        else {
            return "";
        }
    }
    /**
     * 退款
     * @param orderno
     * @param tradeNo
     * @param refundReason
     * @param refundAmount
     * @return
     */
    public static String refund(String oldOrderNo, String orderno, String tradeNo, String refundReason,
            BigDecimal refundAmount) {
        Map<String, String> params = Maps.newHashMap();
        // 基本参数
        // 这部分参数对于一个商户而言是固定的
        params.put(ApiConstants.ORDER_NO, orderno);
        params.put(ApiConstants.PARTNER_ID, Constants.get_partner_id_web());
        params.put(ApiConstants.PROTOCOL, Constants.HTTP_POST_PROTOCOL);
        params.put(ApiConstants.SIGN_TYPE, Constants.SIGN_TYPE);
        params.put(ApiConstants.NOTIFY_URL, Constants.get_op_notify_url());
        params.put(ApiConstants.RETURN_URL, "");

        // 业务参数
        // 每个服务的业务参数是不同的
        // 具体请参考文档的请求参数
        params.put(ApiConstants.SERVICE, Constants.OP_REFUND_SERVICE);
        params.put("tradeNo", tradeNo);
        params.put("refundAmount", refundAmount.toString());
        params.put("refundReason", refundReason);
        params.put("merchOrderNo", oldOrderNo);
        // 发送请求
        String responseMessage = HttpRequest.exchange_web(params);
        logger.info(responseMessage);
        // 反序列化数据
        @SuppressWarnings("unchecked")
        HashMap<String, String> resultMap =
                JsonMapper.nonDefaultMapper().fromJson(responseMessage, HashMap.class);
        if (resultMap.get("resultCode") != null
                && resultMap.get("resultCode").equals("EXECUTE_PROCESSING"))
            return resultMap.get("resultCode");
        else {
            return "";
        }
    }

    public static String reserve(String orderno, String tradename, String payeeUserId,
            BigDecimal totalAmount, int type, int length) {
        Map<String, String> params = Maps.newHashMap();
        // 基本参数
        // 这部分参数对于一个商户而言是固定的
        params.put(ApiConstants.ORDER_NO, orderno);
        params.put(ApiConstants.PARTNER_ID, Constants.get_partner_id_web());
        params.put(ApiConstants.PROTOCOL, Constants.HTTP_POST_PROTOCOL);
        params.put(ApiConstants.SIGN_TYPE, Constants.SIGN_TYPE);
        params.put(ApiConstants.NOTIFY_URL, Constants.get_notify_url());
        params.put(ApiConstants.RETURN_URL, Constants.get_reserve_okurl());

        // 业务参数
        // 每个服务的业务参数是不同的
        // 具体请参考文档的请求参数
        params.put(ApiConstants.SERVICE, "createTradeOrder");
        params.put("requestType", "1");
        params.put("currency", "CNY");
        params.put("tradeName", tradename);
        params.put("tradeAmount", totalAmount.toString());
        params.put("partnerUserId", payeeUserId.split("a")[0]);
        params.put("sellerUserId", Constants.get_partner_id_web());
        /*
         * Map<String, String> goodsClauses = Maps.newHashMap(); goodsClauses.put("name", "设备续费");
         * goodsClauses.put("price", totalAmount.toString()); ObjectMapper mapper = new
         * ObjectMapper(); List<Map<String, String>> ents = new ArrayList<Map<String, String>>();
         * ents.add(goodsClauses); Map<String, String> incidentalFeeClauses = Maps.newHashMap();
         * incidentalFeeClauses.put("payerRole", "device_buyer");
         * incidentalFeeClauses.put("payeeUserId",payeeUserId); try {
         * params.put("goodsClauses",mapper.writeValueAsString(ents));
         * //params.put("incidentalFeeClauses",mapper.writeValueAsString(incidentalFeeClauses)); }
         * catch (Exception e) {
         * 
         * }
         */

        // 发送请求
        String responseMessage = HttpRequest.exchange_web(params);
        // 反序列化数据
        @SuppressWarnings("unchecked")
        HashMap<String, String> resultMap =
                JsonMapper.nonDefaultMapper().fromJson(responseMessage, HashMap.class);
        if (resultMap.get("resultCode") != null
                && resultMap.get("resultCode").equals("EXECUTE_SUCCESS"))
            return resultMap.get("tradeNo");
        else {
            return "";
        }
    }

    public static String resubmit(String orderno, String tradename, String payeeUserId,
            BigDecimal totalAmount) {
        Map<String, String> params = Maps.newHashMap();
        // 基本参数
        // 这部分参数对于一个商户而言是固定的
        params.put(ApiConstants.ORDER_NO, orderno);
        params.put(ApiConstants.PARTNER_ID, Constants.get_partner_id_web());
        params.put(ApiConstants.PROTOCOL, Constants.HTTP_POST_PROTOCOL);
        params.put(ApiConstants.SIGN_TYPE, Constants.SIGN_TYPE);
        params.put(ApiConstants.NOTIFY_URL, Constants.get_notify_url());
        params.put(ApiConstants.RETURN_URL, Constants.get_resumbit_okurl());

        // 业务参数
        // 每个服务的业务参数是不同的
        // 具体请参考文档的请求参数
        params.put(ApiConstants.SERVICE, "createTradeOrder");
        params.put("requestType", "1");
        params.put("currency", "CNY");
        params.put("tradeName", tradename);
        params.put("tradeAmount", totalAmount.toString());
        params.put("partnerUserId", payeeUserId);
        params.put("sellerUserId", Constants.get_partner_id_web());
        // 发送请求
        String responseMessage = HttpRequest.exchange_web(params);
        // 反序列化数据
        @SuppressWarnings("unchecked")
        HashMap<String, String> resultMap =
                JsonMapper.nonDefaultMapper().fromJson(responseMessage, HashMap.class);
        if (resultMap.get("resultCode") != null
                && resultMap.get("resultCode").equals("EXECUTE_SUCCESS"))
            return resultMap.get("tradeNo");
        else {
            return "";
        }
    }

    /*
     * 站内转账
     */
    public static String tradeTransfer(String orderno, String payerId, String payeeId,
            BigDecimal tradeAmount) {
        Map<String, String> params = Maps.newHashMap();
        // 基本参数
        // 这部分参数对于一个商户而言是固定的
        params.put(ApiConstants.ORDER_NO, orderno);
        params.put(ApiConstants.PARTNER_ID, Constants.get_partner_id());
        params.put(ApiConstants.PROTOCOL, Constants.HTTP_POST_PROTOCOL);
        params.put(ApiConstants.SIGN_TYPE, Constants.SIGN_TYPE);
        params.put(ApiConstants.NOTIFY_URL, Constants.get_notify_url());
        params.put(ApiConstants.RETURN_URL, Constants.RETURN_URL);

        // 业务参数
        // 每个服务的业务参数是不同的
        // 具体请参考文档的请求参数
        params.put(ApiConstants.SERVICE, "tradeTransferWarp");
        params.put("tradeName", "站内代扣");
        params.put("payerId", payerId);
        params.put("payeeId", payeeId);
        params.put("tradeAmount", tradeAmount.toString());
        params.put("currency", "CNY");
        params.put("gatheringType", "SERVICE_BUY");

        // 发送请求
        String responseMessage = HttpRequest.exchange(params);
        // 反序列化数据
        @SuppressWarnings("unchecked")
        HashMap<String, String> resultMap =
                JsonMapper.nonDefaultMapper().fromJson(responseMessage, HashMap.class);

        if (resultMap.get("resultCode") != null)
            return resultMap.get("resultCode");
        else {
            return "";
        }
    }
}
